//
//  PywDisclaimerView.m
//  PywSdk
//
//  Created by Ampaw on 2017/7/31.
//  Copyright © 2017年 Jade. All rights reserved.
//

#import "PywDisclaimerView.h"
#import "UIButton+Pyw.h"
#import "PywMasonry.h"
#import "UIImage+Pyw.h"

@interface PywDisclaimerView ()
@property (nonatomic, strong) UIButton    *disclaimerBtn;       // 免责声明按钮
@property (nonatomic, strong) UIButton    *isReadisclaimerBtn;  // 是否已阅读免责声明按钮

@end

@implementation PywDisclaimerView

- (instancetype)initWithDelegate:(id<PywDisclaimerViewDelegate>)delegate
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    [self addSubview:self.disclaimerBtn];
    [self addSubview:self.isReadisclaimerBtn];
    
    WEAK_SELF;
    [self.disclaimerBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.left.bottom.offset(0);
        make.width.mas_equalTo(80);
    }];
    [self.isReadisclaimerBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.left.equalTo(strongSelf.disclaimerBtn.mas_right);
        make.top.bottom.equalTo(strongSelf.disclaimerBtn);
        make.width.mas_equalTo(80);
    }];
}

#pragma mark - Action
// 点击 免责声明
- (void)selectDisclaimerButtonAction:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(didButton:index:)]) {
        [self.delegate didButton:sender index:ButtonTag_Disclaimer];
    }
}
// 点击 是否已阅读
- (void)selectIsDisclaimerButtonAction:(UIButton *)sender
{
    self.isReadisclaimerBtn.selected = !sender.selected;
    if (self.isReadisclaimerBtn.selected) {
        [_isReadisclaimerBtn setImage:[UIImage imageWithName:@"icon_isread_selected"] forState:UIControlStateSelected];
    }else{
        [_isReadisclaimerBtn setImage:[UIImage imageWithName:@"icon_isread_normal"] forState:UIControlStateNormal];
    }
    
    if ([self.delegate respondsToSelector:@selector(didButton:index:)]) {
        [self.delegate didButton:sender index:ButtonTag_IsReadDisclaimer];
    }
}

#pragma mark - Lazy
- (UIButton *)disclaimerBtn
{
    if (!_disclaimerBtn) {
        _disclaimerBtn = [UIButton buttonWithBackgroundColor:[UIColor clearColor]
                                                cornerRadius:0
                                                  titleColor:UIColorFromRGB(0x5A5AAD)
                                          titleLabelFontSize:16
                                                       title:@"免责声明"];
        _disclaimerBtn.tag = ButtonTag_Disclaimer;
        _disclaimerBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_disclaimerBtn addTarget:self action:@selector(selectDisclaimerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_disclaimerBtn setAccessibilityIdentifier:@"common_disclaimer"];
    }
    return _disclaimerBtn;
}

- (UIButton *)isReadisclaimerBtn
{
    if (!_isReadisclaimerBtn) {
        _isReadisclaimerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _isReadisclaimerBtn.backgroundColor = [UIColor clearColor];
        _isReadisclaimerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _isReadisclaimerBtn.tag = ButtonTag_IsReadDisclaimer;
        
        [_isReadisclaimerBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
        [_isReadisclaimerBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 50, 0, 0)];
        
        [_isReadisclaimerBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [_isReadisclaimerBtn setTitle:@"已阅读" forState:UIControlStateNormal];
        [_isReadisclaimerBtn setImage:[UIImage imageWithName:@"icon_isread_normal"] forState:UIControlStateNormal];
        [_isReadisclaimerBtn setImage:[UIImage imageWithName:@"icon_isread_selected"] forState:UIControlStateSelected];
        [_isReadisclaimerBtn addTarget:self action:@selector(selectIsDisclaimerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _isReadisclaimerBtn;
}

@end
